package com.appspot.yourvirtualgymtrainer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			  throws ServletException, IOException {
			  PrintWriter out = resp.getWriter();
			  HttpSession sess = req.getSession(true);

			  System.out.println(sess.getAttribute("isLogged"));
			try {
				if(!sess.getAttribute("isLogged").equals("no")) {
					resp.setContentType("application/json; charset=utf-8");
					resp.setHeader("Cache-Control", "no-cache");
				} else {
					resp.setContentType("text/plain; charset=utf-8");
					out.println("niezalogowany");
				}
		  } catch (NullPointerException e) {
			  sess.setAttribute("isLogged", "no");
			  resp.setContentType("text/plain; charset=utf-8");
			  out.println("niezalogowany");
		  }
	 }
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		    throws ServletException, IOException {
		 System.out.println(req.getParameter("login") + " " + req.getParameter("password"));
		 resp.setContentType("text/plain; charset=utf-8");
		 resp.setHeader("Cache-Control", "no-cache");
		 req.setAttribute("zalogowano","tak");
		 req.getRequestDispatcher("/admin.jsp").forward(req, resp); 
		// resp.sendRedirect("/index.html");
	}
	
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
		    throws ServletException, IOException {
		 
		  System.out.println(req.getParameter("login") + " " + req.getParameter("password"));
	}
}
